// Game.cpp: implementation of the CGame class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Game.h"
#include "RFK2_2.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSize CGame::m_Offset=CSize(0,0);
void CGame::Initialize()
{
	for(int i=0;i<GAME_HEIGHT ;i++)
		for(int j=1;j<GAME_WIDTH+1;j++)
			m_Board[i][j]=0;
		for(int j=0;j<GAME_HEIGHT+1;j++)
		{
			m_Board[j][0]=-1;
			m_Board[j][GAME_WIDTH+1]=-1;
		}
		for(i=0;i<GAME_WIDTH+2;i++)
			m_Board[GAME_HEIGHT][i]=-1;
}
void CGame::OutPut()
{
	for(int i=0;i<GAME_HEIGHT+1;i++)
	{
		for(int j=0;j<GAME_WIDTH+2;j++)
           TRACE("%d ",m_Board[i][j]);
		TRACE("\n");
	}

}
CGame::CGame()
{m_GameState=Start;
 m_RemovedLines=0;
}

CGame::~CGame()
{

}

void CGame::PlaceBlock(CBlock& block)
{
	int color=block.GetColorIndex()+1;
	for(int i=0;i<block.GetSize();i++)
		for(int j=0;j<block.GetSize();j++)
			if( (block.GetElem())[4*i+j]=='*')
				m_Board[block.GetPosition().y+i][block.GetPosition().x+j+1]=color;

}

void CGame::RemoveLines()
{	int i,j,k,l;
	bool lineFull;
	int  linescount;

	linescount = 0;
	j=20-1;
	while (j>=0)
	{
		lineFull=true;
		for (i=1;i<=GAME_WIDTH;i++)
		if (m_Board[j][i]==0)
			lineFull=false;
		if (lineFull)
		{
			linescount++;
			for (k=j;k>=1;k--)
				for (l=1;l<=GAME_WIDTH;l++)
					m_Board[k][l]=m_Board[k-1][l];
			for (l=1;l<=1;l++)
				m_Board[0][l]=0;
		}
	    else
		j--;
	}
	m_RemovedLines+=linescount;
}
void CGame::Display(CDC &dc)
{	
	
  
	for(int i=0;i<GAME_HEIGHT;i++)
		for(int j=1;j<GAME_WIDTH+1;j++)
			if(m_Board[i][j]!=0)
			{   COLORREF color=COLOR[m_Board[i][j]-1];
			    CBrush brush;
				brush.CreateSolidBrush(color);
				CRect rect(CPoint(CBlock::BLOCK_SIZE*(j-1)+CBlock::m_Offset.cx,
					CBlock::BLOCK_SIZE*i+CBlock::m_Offset.cy),
				CSize(CBlock::BLOCK_SIZE,CBlock::BLOCK_SIZE));
			dc.FillRect(&rect,&brush);
			dc.Draw3dRect(&rect,GetTopLeftColor(color),GetBottomRightColor(color));
			}
}

void CGame::ChangState(GameState state)
{m_GameState=state;

}

GameState CGame::GetState()
{return m_GameState;
}
void CGame::SetOffset(int x,int y)
{
m_Offset.cx=x;
m_Offset.cy=y;
}

int CGame::GetRemovedLines()
{
return m_RemovedLines;
}

void CGame::SetRemovedLines(int lines)
{
m_RemovedLines=lines;
}
